package com.harveyguo.nihonngo.repositories;

import com.harveyguo.nihonngo.enums.EventLevel;
import com.harveyguo.nihonngo.model.entity.Event;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 赛事Repository
 */
@Repository
public interface EventRepository extends JpaRepository<Event, String> {

    /**
     * 根据拳馆ID查找赛事
     */
    List<Event> findByBoxingGymId(String gymId);

    /**
     * 根据赛事级别查找
     */
    List<Event> findByLevel(EventLevel level);

    /**
     * 根据活跃状态查找赛事
     */
    List<Event> findByIsActive(Boolean isActive);

    /**
     * 根据状态查找赛事
     */
    List<Event> findByStatus(String status);

    /**
     * 查找即将开始的赛事
     */
    @Query("SELECT e FROM Event e WHERE e.startDate >= :startDate AND e.isActive = true ORDER BY e.startDate")
    List<Event> findUpcomingEvents(@Param("startDate") Date startDate);

    /**
     * 根据地理位置查找附近的赛事
     */
    @Query("SELECT e FROM Event e WHERE " +
            "SQRT(POWER(e.latitude - :latitude, 2) + POWER(e.longitude - :longitude, 2)) <= :radius " +
            "AND e.isActive = true")
    List<Event> findNearbyEvents(@Param("latitude") Double latitude,
            @Param("longitude") Double longitude,
            @Param("radius") Double radius);
}